package com.sanhe.rfmuscle.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sanhe.rfmuscle.databinding.FragmentProBinding;
import com.sanhe.rfmuscle.event.Event;
import com.sanhe.rfmuscle.event.MFEvent;
import com.sanhe.rfmuscle.event.MTEvent;
import com.sanhe.rfmuscle.event.SFEvent;
import com.sanhe.rfmuscle.event.STEvent;
import com.sanhe.rfmuscle.util.Const;
import com.sanhe.rfmuscle.view.LongClickImageView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ProFragment extends BaseWorkFragment {

    FragmentProBinding binding;

    Handler h = new Handler();
    Runnable runnable;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentProBinding.inflate(inflater, container, false);


        binding.mainFreqReduce.setLongClickListener(new LongClickImageView.LongClickListener() {
            @Override
            public void onActionDown() {
                startHandler((byte) 1, (byte) 0x30);
            }

            @Override
            public void onActionUp() {
                stopHandler();
            }
        });

        binding.mainFreqAdd.setLongClickListener(new LongClickImageView.LongClickListener() {
            @Override
            public void onActionDown() {
                startHandler((byte) 1, (byte) 0x31);
            }

            @Override
            public void onActionUp() {
                stopHandler();
            }
        });


        binding.mainTimeReduce.setLongClickListener(new LongClickImageView.LongClickListener() {
            @Override
            public void onActionDown() {
                startHandler((byte) 1, (byte) 0x34);
            }

            @Override
            public void onActionUp() {
                stopHandler();
            }
        });


        binding.mainTimeAdd.setLongClickListener(new LongClickImageView.LongClickListener() {
            @Override
            public void onActionDown() {
                startHandler((byte) 1, (byte) 0x35);
            }

            @Override
            public void onActionUp() {
                stopHandler();
            }
        });


        binding.subFreqReduce.setLongClickListener(new LongClickImageView.LongClickListener() {
            @Override
            public void onActionDown() {
                startHandler((byte) 1, (byte) 0x32);
            }

            @Override
            public void onActionUp() {
                stopHandler();
            }
        });


        binding.subFreqAdd.setLongClickListener(new LongClickImageView.LongClickListener() {
            @Override
            public void onActionDown() {
                startHandler((byte) 1, (byte) 0x33);
            }

            @Override
            public void onActionUp() {
                stopHandler();
            }
        });


        binding.subTimeReduce.setLongClickListener(new LongClickImageView.LongClickListener() {
            @Override
            public void onActionDown() {
                startHandler((byte) 1, (byte) 0x36);
            }

            @Override
            public void onActionUp() {
                stopHandler();
            }
        });


        binding.subTimeAdd.setLongClickListener(new LongClickImageView.LongClickListener() {
            @Override
            public void onActionDown() {
                startHandler((byte) 1, (byte) 0x37);
            }

            @Override
            public void onActionUp() {
                stopHandler();
            }
        });

        updatePro1();
        updatePro2();
        updatePro3();
        updatePro4();
        return binding.getRoot();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MFEvent event) {
        updatePro1();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MTEvent event) {
        updatePro2();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SFEvent event) {
        updatePro3();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(STEvent event) {
        updatePro4();
    }

    private void updatePro1() {
        if (Event.mfEvent.getValue() != null) {
            int pro1 = Event.mfEvent.getValue();
            binding.mainFreqSeekbar.setProgress(pro1);
            binding.mainFreq.setText(pro1 + "");
        }
    }

    private void updatePro2() {
        if (Event.mtEvent.getValue() != null) {
            int pro2 = Event.mtEvent.getValue();
            binding.mainTimeSeekbar.setProgress(pro2);
            binding.mainTime.setText(pro2 + "");
        }
    }

    private void updatePro3() {
        if (Event.sfEvent.getValue() != null) {
            int pro3 = Event.sfEvent.getValue();
            binding.subFreqSeekbar.setProgress(pro3);
            binding.subFreq.setText(pro3 + "");
        }
    }


    private void updatePro4() {
        if (Event.stEvent.getValue() != null) {
            int pro4 = Event.stEvent.getValue();
            binding.subTimeSeekbar.setProgress(pro4);
            binding.subTime.setText(pro4 + "");
        }
    }


    private void startHandler(byte data_H, byte data_L) {
        if (runnable != null) h.removeCallbacks(runnable);
        runnable = new Runnable() {
            @Override
            public void run() {
                sendTouchEvent((byte) data_H, (byte) data_L);
                h.postDelayed(runnable, 150);
            }
        };
        h.post(runnable);

    }

    private void stopHandler() {
        h.removeCallbacks(runnable);
    }

    @Override
    public void onDestroy() {
        if (h != null) h.removeCallbacksAndMessages(null);
        super.onDestroy();
    }

}
